package com.controller;

import com.pojo.Files;
import com.pojo.Page;
import com.pojo.User;
import com.service.UserService;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import com.util.PageTool;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.support.AbstractMultipartHttpServletRequest;
import org.springframework.web.servlet.HttpServletBean;

import javax.annotation.Resource;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @RequestMapping("/login")
    @ResponseBody
    public String login(String loginname,String password){
        User user = userService.login(loginname,password);
        if(user!=null){
            return "{\"fig\":\"success\"}";
        }
        else{
            return "{\"fig\":\"error\"}";
        }
    }
    @RequestMapping("/queryall1")
    @ResponseBody
        public Page queryall(int pageindex){
        Page page=new Page(5,pageindex);
        List<User>list=userService.queryFen(page.getStartIndex(),page.getEndIndex());
        int row=userService.countall(null);
        page.setResult(list,row);
        return page;
    }
    @RequestMapping("/deleteByID")
    @ResponseBody
    public String deleteByID(int id){
        int result = userService.deleteUser(id);
        if(result>0){
            return "{\"fig\":\"success\"}";
        }
        else{
            return "{\"fig\":\"error\"}";
        }
    }
    @RequestMapping("/updateInfo")
    @ResponseBody
    public String updateInfo(@RequestBody User user){
        int result = userService.updateUser(user);
        if (result > 0) {
            return "{\"fig\":\"success\"}";
        }
        else{
            return "{\"fig\":\"error\"}";

        }
    }
    @RequestMapping("/querybyuserid")
    @ResponseBody
    public User querybyUserid(int id){
        User user = userService.queryByUserid(id);
        return user;
    }
    @RequestMapping("/updatepass")
    @ResponseBody
    public String updatepass(String loginname,String oldpass,String newpass){
        int result = userService.updatePass(loginname, oldpass, newpass);
        if(result>0){
            return "{\"fig\":\"success\"}";
        }
        else{
            return "{\"fig\":\"error\"}";

        }
    }
    @RequestMapping("/reg")
    @ResponseBody
    public String reg(@RequestBody User user){
        User user1 = userService.querybyname(user.getLoginname());
        if(user1!=null) {
            return "{\"fig\":\"error\"}";
        }
        else{
            int result = userService.regist(user);
            if (result > 0) {
                return "{\"fig\":\"success\"}";
            } else {
                return "{\"fig\":\"error\"}";

            }
        }
    }
    @RequestMapping("/querybyname")
    @ResponseBody
    public Page queryByName(Integer pageIndex,String username){
        Page page=new Page(5,pageIndex);
        List<User> list= userService.queryByUsername(username,page.getStartIndex(),page.getEndIndex());
        int row=userService.countall(username);
        page.setResult(list,row);
        return page;
    }
    @RequestMapping("/querytorole")
    @ResponseBody
    public User queryToRole(String loginname){
        User user = userService.queryToRole(loginname);
        System.out.println(user.getRole());
        return user;
    }
}
